package com.googlecode.imanager.swing.form;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComponent;

import com.googlecode.imanager.client.Document;
import com.googlecode.imanager.client.Manager;
import com.googlecode.imanager.client.bridge.IFocusable;
import com.googlecode.imanager.client.form.FocusControl;
import com.googlecode.imanager.core.base.BridgeApplication;
import com.googlecode.imanager.swing.SwingControl;
import com.googlecode.imanager.swing.SwingDocument;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class SwingFocusControl extends SwingControl implements IFocusable {

    public SwingFocusControl(BridgeApplication bridge, FocusControl source) {
        super(bridge, source);
    }
    
    public JComponent getTargetFocus(){
        return (JComponent) target;
    }

    @Override
    protected void doAttachEvents() {
        super.doAttachEvents();
        doAttachFocusEvents();
    }

    protected void doAttachFocusEvents() {
        getTargetFocus().addFocusListener(new FocusListener() {
            
            @Override
            public void focusLost(FocusEvent e) {
                synchronized (TREE_LOCK) {
                    if (!isDropped()){
                        startFireUIEvent();
                        try {
                            getManager().fireBlur(getSource());
                        } catch (Exception ex) {
                            processException(ex);
                        } finally {
                            endFireUIEvent();
                        }
                    }
                }
            }
            
            @Override
            public void focusGained(FocusEvent e) {
                synchronized (TREE_LOCK) {
                    if (!isDropped()){
                        startFireUIEvent();
                        try {
                            getManager().fireFocus(getSource());
                        } catch (Exception ex) {
                            processException(ex);
                        } finally {
                            endFireUIEvent();
                        }
                    }
                }
            }
            
        });
    }

    @Override
    public FocusControl getSource() {
        return (FocusControl) source;
    }

    @Override
    public void focus() {
        getTargetFocus().requestFocusInWindow();
    }
    
    @Override
    public void blur() {
        Document doc = getSource().getRootDocument();
        if (doc != null){
            SwingDocument window = (SwingDocument) Manager.getImpl(doc);
            window.getTarget().requestFocusInWindow();
        }
    }

    @Override
    public void setTabIndex(Integer tabIndex) {
        getTarget().setFocusable(tabIndex == null || tabIndex >= 0);
        
        // TODO definir a ordem do focus
    }

    @Override
    public void setAccessKey(Character accessKey) {
        // TODO Auto-generated method stub
    }

    @Override
    public void transferFocusBackward() {
        getTargetFocus().transferFocusBackward();
    }

    @Override
    public void transferFocusForward() {
        getTargetFocus().transferFocus();
    }

}
